package com.example.util;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class AutoServiceUtil {

    private static Map<String, String> FIEL_MAP = new HashMap<>();

    /**
     * 生成service 和 mapper
     *
     * @param filePath    项目路径
     * @param serviceName 需要在哪个service 生成
     * @param retType     返回类型
     * @param funName     生成的方法名
     * @return
     */
    public static Map<String, Object> auto(File filePath, String serviceName,
                                           String retType, String funName) {

        return null;
    }

    /**
     * 获取service 前缀
     *
     * @param serviceName
     * @return
     */
    private static String getServicePrefix(String serviceName) {
        int idx = serviceName.indexOf("Service");
        serviceName = serviceName.substring(0, idx);
        return serviceName;
    }

    public static void heq(File file, Map<String, String> map) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File file1 : files) {
                if (file1.isDirectory()) {
                    heq(file1, map);
                } else {
                    String v = map.get(file1.getName());
                    if (null != v && !"".equals(v)) {
                        FIEL_MAP.put(v, file1.getAbsolutePath());
                        System.out.println(file1.getName());
                        System.out.println(file1.getAbsolutePath());
                    }
                }
            }
        }
    }

    public static Map<String, String> getFileMap(File file, String serviceName,
                                                 String[] scsz) {
        FIEL_MAP = null;
        long start = System.currentTimeMillis();
        if (null == FIEL_MAP) {
            FIEL_MAP = new HashMap<>();
        }
//        String prefix = getServicePrefix(serviceName);
        Map<String, String> map = new HashMap<>();
        boolean bl = false;
        for (String str : scsz) {
            String fileName = serviceName + str + ".java";
            if (null == FIEL_MAP.get(fileName)) {
                bl = true;
            }
            map.put(fileName, str);
        }
        if (bl) {
            // 递归形式查询文件
            heq(file, map);
        }

        long end = System.currentTimeMillis();
        System.out.println("查找文件用时：" + ((end - start) / 1000) + "秒");
        return FIEL_MAP;
    }

    public static void main(String[] args) {
//        File file = new File("D:\\yihe_work\\qiklwork\\xiye-esc\\xiye-operate\\src\\main\\java\\com\\xiye\\operate\\service\\blxwsjsh");
        File file = new File("C:\\test");
//        getFileMap(file, "BlxwsjshService");
        Map<String, String> map = new HashMap<>();
        map.put("userName", "用户名");
        map.put("password", "密码");
        for (Map.Entry<String, String> entry : FIEL_MAP.entrySet()) {
            String template = getTemplate(entry.getKey(), "jiwd", "我的测试方法", map,
                    "String");
//            try {
//                FileWriter fw = new FileWriter(entry.getValue(), true);
//                fw.write(template);
//                fw.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            try {
                RandomAccessFile accessFile = new RandomAccessFile(entry.getValue(), "rw");
                System.out.println(accessFile.length());
                accessFile.seek(accessFile.length()); // 定位文件指针所指的位置
                long pos = accessFile.length() - 1; // 定位文件尾
//                accessFile.seek(pos); // 定位文件指针所指的位置
                boolean bl = true;
                while (bl) { // 判断文件是否到达头
                    --pos; // 一个字符一个字符的向前移动指针
                    accessFile.seek(pos); // 定位文件指针所指的位置
                    if (accessFile.readLine().indexOf("}") > -1) { // 如果是换行符,
                        System.out.println("++++++++++" + pos);
                        // 就可以读取该行了
                        System.out.println(accessFile.readLine());
                        bl = false;
                    }
                }
                System.out.println("------------" + pos);
                accessFile.seek(pos);
                accessFile.write(template.getBytes());
                accessFile.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    private static String getTemplate(String fileName, String autor, String ms,
                                      Object param,
                                      String ret) {
        Date data = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuilder sb = new StringBuilder();
        if (null != param || "" != param) {
            if (param instanceof Map) {
                Map<String, String> pm = (Map<String, String>) param;
                for (Map.Entry<String, String> entry : pm.entrySet()) {
                    sb.append("     * @Param " + entry.getKey() + ":" + entry.getValue() + "\n");
                    System.out.println("方法五的key为：" + entry.getKey() + ",value为：" + entry.getValue());
                }
            } else if (param instanceof String) {
                sb.append("     * @Param " + param + "\n");
            }
        }
        String impl = " ";
        if (fileName.toLowerCase().indexOf("impl") > -1) {
            impl = "     @Override\n";
        }
        String template = "\n\n     /**\n" +
                "     * @Author " + autor + "\n" +
                "     * @Date " + df.format(data) + "\n" +
                "     * @Description " + ms + "\n" +
                sb.toString() +
                "     * @Return " + ret + "\n" +
                "     */\n" + impl + "\n";
        System.out.println(template);
        return template;
    }

}
